package org.jypj.dev.controller;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.jypj.dev.model.GiveWork;
import org.jypj.dev.model.TrainNumInfo;
import org.jypj.dev.service.TrainNumInfoService;
import org.jypj.dev.util.ExcelHelper;
import org.jypj.dev.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车组号信息
 */
@Controller
@RequestMapping("trainNumInfo")
public class TrainNumController extends BaseController {

    @Resource
    private TrainNumInfoService trainNumInfoService;

    /**
     * 跳转到车组号信息页面
     *
     * @return
     */
    @RequestMapping(value = "toTrainNumInfoList.action")
    public String toTrainNumInfoList() {

        return "back/sys/train_num_info_list";
    }

    /**
     * 分页查询
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectOnePage.action")
    public Page selectOnePage(Page page, String searchKey) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("searchKey", searchKey);
        page = trainNumInfoService.selectOnePageByMap(page, queryMap);
        return page;
    }

    /**
     * 查询所有车组号信息
     */
    @RequestMapping("exportTrainNumInfos.action")
    public void exportGiveWorks() {
        Map<String, Object> queryMap = new HashMap<>();
        List<TrainNumInfo> trainNumInfos = trainNumInfoService.selectAllByMap(queryMap);
        if (trainNumInfos != null) {
            exportExcel(trainNumInfos, "trainNumInfo", getRequest().getSession(), getResponse());
        }
    }


    /**
     * 导出excel
     *
     * @param title
     * @param session
     * @param response
     */
    public void exportExcel(List<TrainNumInfo> trainNumInfos, String title, HttpSession session,
                            HttpServletResponse response) {

        WritableWorkbook workbook_temp = null;// Excel数据组装
        String usersTempPath = session.getServletContext().getRealPath("/static/excelModel/") + File.separator
                + "trainNumInfo_temp.xls";
        String userPath = session.getServletContext().getRealPath("/static/excelModel/") + File.separator
                + "trainNumInfo.xls";
        Workbook workbook = null; // 工作表
        try {
            workbook = Workbook.getWorkbook(new File(userPath)); // 创建一张工作表从实体路径下
            workbook_temp = Workbook.createWorkbook(new File(usersTempPath), workbook); // 拷贝一份模板
            WritableSheet sheet = workbook_temp.getSheet(0);
            for (int i = 0; i < trainNumInfos.size(); i++) {
                TrainNumInfo trainNumInfo = trainNumInfos.get(i);
                sheet.addCell(new Label(0, 3 + i, i + 1 + ""));
                sheet.addCell(new Label(1, 3 + i, trainNumInfo.getCreateTime()));
                sheet.addCell(new Label(2, 3 + i, trainNumInfo.getTrainType()));
                sheet.addCell(new Label(3, 3 + i, trainNumInfo.getId()));
                sheet.addCell(new Label(4, 3 + i, trainNumInfo.getDqzxgl()));
                sheet.addCell(new Label(5, 3 + i, trainNumInfo.getScxxzxgls()));
                sheet.addCell(new Label(6, 3 + i, trainNumInfo.getScxxhzxgls()));
                sheet.addCell(new Label(7, 3 + i, trainNumInfo.getScejxzxgls()));
                sheet.addCell(new Label(8, 3 + i, trainNumInfo.getQtejxxmfb()));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 关闭Excel工作薄对象
            if (workbook_temp != null) {
                try {
                    workbook_temp.write();
                    workbook_temp.close();
                    ExcelHelper.outputExcel(response, title, usersTempPath);
                    new File(usersTempPath).delete();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (workbook != null) {
                workbook.close();
            }
        }

    }
}
